/**
 * 
 */
var geocodeTimer;
var FTUserRouteLayer=null;
var distanceWidget=null;
var maxDistance=0;
var join=1;
var initPosFlag = new Boolean(false);
var initPos;
var initPosMarker;
var endPos;
var ruta=0;
var r1Id="--";
var r1LatLong="--";
var r2Id="--";
var r2LatLong="--";
var r3Id="--";
var r3LatLong="--";
var r4Id="--";
var r4LatLong="--";

var initFunction = function initialize() {
	//Get the user location via W3C standard
	var initialLocation = new google.maps.LatLng(42.846406, -2.667893);
	//Create the map
	createMap(initialLocation, "auto");
	
	setBlueStyle();
	
	displayFTRoutes(distanceWidget);

	initialLocation = locateUser();
	
	
};

function displayFTRoutes(widget){
	

		FTUserRouteLayer = new google.maps.FusionTablesLayer(323755,{
			query: "SELECT route FROM 323755"
		});
		

	FTUserRouteLayer.setMap(map);
	
	google.maps.event.addListener(FTUserRouteLayer, 'click', joinRoutes );
	
}

function joinRoutes(table) {
	
	//Cargamos la Ruta que ha clickado
	
	//alert(table.row['description'].value);
	
	//Se obtiene la Id de la ruta selecionada
	
	if (join==1){
		
		if (ruta==0){
		
			r1Id=table.row['id'].value;
			r1LatLong=table.latLng;
			
			initPosMarker = new google.maps.Marker({
			    position: table.latLng,
			    map: map
			});
		
			initPos=table.latLng;
			ruta = 1;
			
		    document.getElementById('r1Id').value=r1Id;
			document.getElementById('r1LatLong').value=r1LatLong;
	
		}else if (ruta ==1){
		
		
			r2Id=table.row['id'].value;
			r2LatLong=table.latLng;
			
			initPosMarker = new google.maps.Marker({
			    position: table.latLng,
			    map: map
			});
		
			document.getElementById('r2Id').value=r2Id;
			document.getElementById('r2LatLong').value=r2LatLong;
	
			ruta = 2;
	}
	else if (ruta ==2){
		
		
			r3Id=table.row['id'].value;
			r3LatLong=table.latLng;
			
			initPosMarker = new google.maps.Marker({
			    position: table.latLng,
			    map: map
			});
		
			document.getElementById('r3Id').value=r3Id;
			document.getElementById('r3LatLong').value=r3LatLong;
	
			ruta = 3;
	}
	else if (ruta ==3){
		
		
			r4Id=table.row['id'].value;
			r4LatLong=table.latLng;
			
			initPosMarker = new google.maps.Marker({
			    position: table.latLng,
			    map: map
			});
		
			document.getElementById('r4Id').value=r4Id;
			document.getElementById('r4LatLong').value=r4LatLong;
	
			ruta = 0;
	}
	
}

}

